package service

import org.paic.d.D
import org.paic.d.Pager
import util.Utils

import org.slf4j.Logger
import org.slf4j.LoggerFactory

// 用户服务
class UserService {
	static final String USER_COOKIE = 'ibid-user'

	static final String USER_ADD_URL = 'http://mp.weixin.qq.com/s?__biz=MzA3NTg5ODExMQ==&mid=202243286&idx=1&sn=a4b3745de76f997f0e17ad0472b3c183#rd'

	static Logger log = LoggerFactory.getLogger(UserService.class)

	// 登录
	static Map login(int companyId, String pwd, boolean isRaw){
		def dao = D.gen()

		dao.queryMap('select id as company_id, name from T_IBID_CARD_COMPANY where id = ? and pwd = ?', 
			[companyId, isRaw ? Utils.encode(pwd) : pwd])
	}

	static Map loginByOpenid(String openid, String pwdEncoded){
		def dao = D.gen()

		dao.queryMap('select id as company_id, name from T_IBID_CARD_COMPANY where openid_manager = ? and pwd = ?', 
			[openid, pwdEncoded])	
	}

	// 修改密码
	static void changePwd(int companyId, String pwd){
		def dao = D.gen()

		dao.update([id: companyId, pwd: Utils.encode(pwd)], 'T_IBID_CARD_COMPANY', null)
	}

	// 添加一个公司，并设置管理用户，默认密码，默认一个打卡器
	static Map create(String openid, String name){
		def item = [:]

		def dao = D.gen()
		def one = dao.queryMap('select id from T_IBID_CARD_COMPANY where name = ?', [name])
		if(one){
			item.error = 'company-already-exist'
			return item
		}

		// 默认的密码
		String pwd = '123456'
		pwd = Utils.encode(pwd)
		int companyId = dao.add([name: name, pwd: pwd, openidManager: openid], 'T_IBID_CARD_COMPANY')
		
		// 默认的打卡器名称
		String scanerName = 'A卡'
		dao.add([companyId: companyId, name: scanerName], 'T_IBID_CARD_SCANER')

		joinUser(openid, companyId)

		item.pwd = pwd
		item.companyId = companyId
		item
	}

	static boolean isUserExists(String openid){
		def dao = D.gen()
		def user = dao.queryMap('select id from T_IBID_CARD_USER where openid = ? limit 1', [openid])

		user != null
	}

	static String createUserIfNotExists(String openid){
		def dao = D.gen()
		def user = dao.queryMap('select name from T_IBID_CARD_USER where openid = ? limit 1', [openid])

		String userName
		if(!user){
			dao.add([openid: openid], 'T_IBID_CARD_USER')
		}else{
			userName = user.name
		}

		userName
	}

	// 获取个人信息，包括公司，姓名
	static Map getUserWithCompany(String openid){
		def dao = D.gen()
		def user = dao.queryMap('select name, company_id from T_IBID_CARD_USER where openid = ? limit 1', [openid])
		if(!user){
			return null
		}

		// 不用left join，方便以后分表
		if(user.companyId){
			def one = dao.queryMap('select name from T_IBID_CARD_COMPANY where id = ? limit 1', [user.companyId])
			if(one){
				user.companyName = one.name
			}else{
				user.companyName = ''
			}
		}else{
			user.companyName = ''
		}

		user
	}

	// 设置个人姓名
	static void setUserName(String openid, String name){
		def dao = D.gen()
		def user = dao.queryMap('select id from T_IBID_CARD_USER where openid = ? limit 1', [openid])
		if(!user){
			dao.add([openid: openid, name: name], 'T_IBID_CARD_USER')
		}else{
			dao.update([name: name, id: user.id], 'T_IBID_CARD_USER', null)
		}
	}

	// 设置用户cookie，针对还未接入微信登录的情况，用于标示唯一用户
	static void setUserCookie(String openid, String cookie){
		def dao = D.gen()
		dao.update([cookie: cookie, openid: openid], 'T_IBID_CARD_USER', 'openid')
	}

	static String getOpenidByCookie(String cookie){
		def dao = D.gen()
		def user = dao.queryMap('select openid from T_IBID_CARD_USER where cookie = ? limit 1', [cookie])

		return user ? user.openid : null
	}

	// 关联一个用户到一个公司
	// 返回公司名称
	static String joinUser(String openid, int companyId){
		def dao = D.gen()
		def item = dao.queryMap('select name from T_IBID_CARD_COMPANY where id = ? limit 1', [companyId])

		if(!item)
			return null

		def one = dao.queryMap('select id from T_IBID_CARD_USER where openid = ? limit 1', 
			[openid])
		if(one){
			// update
			dao.update([companyId: companyId, id: one.id], 'T_IBID_CARD_USER', null)
		}else{
			// add
			dao.add([companyId: companyId, openid: openid], 'T_IBID_CARD_USER')
		}

		item.name
	}

	// 获取公司管理者用户信息
	static Map getUserAsManager(String openid){
		def dao = D.gen()
		def item = dao.queryMap('select id as company_id, pwd from T_IBID_CARD_COMPANY where openid_manager = ? limit 1', 
			[openid])

		item
	}

	// *** 留言
	static void leaveMsg(String msg){
		def dao = D.gen()
		dao.add([msg: msg], 't_ibid_leave_msg')
	}

	static Pager getLeaveMsgList(int pageNum, int pageSize){
		def dao = D.gen()

		String sql = 'select * from t_ibid_leave_msg order by id desc'
		dao.pagi(sql, null, pageNum, pageSize)
	}
}